Ext.define('PJ.view.unitregistrationmanagement.UnitChooseUserList', {
    extend: 'AM.default.DefaultList',
	alias: 'widget.unitChooseUserList',
    
    initComponent: function(){
    	var me = this;
        this.columns = [{
            header: '审核状态',
            dataIndex: 'c.auditing_State',
            width: 80,
            //sortable: false,
            renderer: function(v, meta, record){
            	meta.style="padding-bottom: 0px;padding-top: 0px;line-height: 19px;padding-left: 0px;";
                var text = "",tipText="";
                if (v == 3) {
                	tipText="报名待审核";
                	text= '<div style="border-left:5px solid white;height:19px;padding-left:2px;">报名待审核</div>';
                }
                else if (v == 1) {
                	text= '<div style="border-left:5px solid green;height:19px;padding-left:2px;">报名通过</div>';
                	tipText="报名通过";
                }else if (v == 2) {
                	tipText="点击查看驳回原因";
                	text= '<div style="border-left:5px solid red;height:19px;cursor: pointer;padding-left:2px;"><span style="cursor: pointer;color:blue;">报名未通过</span></div>';
                }else if(v == 4){
                	tipText="撤销待审核";
                	text= '<div style="border-left:5px solid white;height:19px;padding-left:2px;">撤销待审核</div>';
                }else if(v == 5){
                	tipText="报名通过(撤销未通过)";
                	text= '<div style="border-left:5px solid green;height:19px;padding-left:2px;">报名通过(<span style="cursor: pointer;color:blue;">撤销未通过</span>)</div>';
                }
                meta.tdAttr = 'data-qtip="' + tipText + '" auditStatus=' + record.get("c.auditing_State") + ' classUserId=' + record.get("classUserId") + ' userId=' + record.get("userId");
                return text;
            },
    		listeners: {
    			click: function(g, clm){
//    				var userId = clm.getAttribute("userId");
    				var auditStatus = clm.getAttribute("auditStatus");
    				var classUserId = clm.getAttribute("classUserId");
    				if(auditStatus == 5){
    					var win = Ext.create('PJ.view.registrationmanagement.CheckEdit', {
        					title: "查看驳回原因",
        					flag:4,
        					grid: me,
        					isDisable:false,
        					isReadOnly:true
        				}).show();
        				var actionName = "load";
        				if(gj_onlineLearning_constant.loadType == "jsonp"){
        					actionName = "loadJsonp";
        				}
        				win.down("form").getForm().doAction(actionName,{
        					url:me.store.findRejectReasonUrl,
        					params:{
        						classUserId: classUserId
        					},
        					success:function(f,action){
        					}
        				});
    				}else if(auditStatus == 2){
    					var win = Ext.create('PJ.view.trainingmanagement.CheckEdit', {
        					title: "查看驳回原因",
        					flag:4,
        					grid: me,
        					isDisable:false,
        					isReadOnly:true
        				}).show();
        				var actionName = "load";
        				if(gj_onlineLearning_constant.loadType == "jsonp"){
        					actionName = "loadJsonp";
        				}
        				win.down("form").getForm().doAction(actionName,{
        					url:me.store.findCommentUrl,
        					params:{
        						classUserId: classUserId
        					},
        					success:function(f,action){
        					}
        				});
    				}else{
    					return;
    				}
    			}
    		}
        },{
        	header: '学员帐号',
        	dataIndex: 'bu.login_id',
        	width: 75
        },{
        	header: '姓名',
        	dataIndex: 'bu.user_Name',
        	width: 75
        }, {
            header: '性别',
            dataIndex: 'bb.order_num',
            width: 45
        },{
            header: '出生年月',
            dataIndex: 'c.birthday',
            width: 80,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: '民族',
            dataIndex: 'bt.order_num',
            width: 45,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },{
            header: '手机号',
            dataIndex: 'c.mobile_Telephone',
            width: 100
        },/*{
            header: '单位',
            dataIndex: 'd.depname',
            width: 120,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },*/{
            header: '职级',
            dataIndex: 'bbb.order_num',
            width: 120,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: '单位及职务',
            dataIndex: 'c.duty_remark',
            flex: 1,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },/* {
            header: '联系电话',
            dataIndex: 't.mobileTelephone',
            width: 90,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },*/ {
            header: '备注',
            dataIndex: 'c.remark',
            flex:.5,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }];
        this.store = Ext.create('PJ.'+prototype+'.unitregistrationmanagement.UnitChooseUserList', {
            isLoad: false
        });
        //实现列表字段的编辑
        this.plugins = [Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1,
            listeners: {
                beforeedit: function(editor, e){
                    var textfield = e.column.getEditor();
                    if(gj_onlineLearning_constant.loadType == "jsonp"){
                    	var storeToken = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
                    		loadType: gj_onlineLearning_constant.loadType,
                    		url: gj_onlineLearning_constant.tokenUrl
                    	});
                    	storeToken.load(function(rec, operation, success){
                    		var token = operation.response;
                    		textfield.token = token;
                    	});
                    }else if(gj_onlineLearning_constant.loadType == "ajax"){
                    	Ext.Ajax.request({
                            url: tokenUrl,
                            success: function(response){
                                var data = response.responseText;
                                var token = Ext.decode(data, true);
                                textfield.token = token;
                            }
                        });
                    }
                }
            }
        })];
        this.loadType = gj_onlineLearning_constant.loadType;
        this.callParent(arguments);
    },

    //报名
    onAddClick: function(button){
        var me = this;
        if(me.signUpState == 2){
        	Ext.Msg.show({
                title: '提示',
                msg: '该班级未开始报名！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
        	return;
        }else if(me.signUpState == 3){
        	Ext.Msg.show({
                title: '提示',
                msg: '该班级已结束报名！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
        	return;
        }else{
        	Ext.create('PJ.view.registrationmanagement.ChooseUserEdit',{
        		title: "报名信息",
        		flag:1,
        		grid: me,
        		openState: me.up('window').openState,//培训班状态
        		classId: me.up('window').classId,//培训班Id
        	}).show();
        }
    },
    //修改
    onUpdateClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
		if(records.length > 1){
			Ext.Msg.show({
				title: '提示',
				msg: '只允许操作一条数据！',
				icon: Ext.Msg.WARNING,
				buttons: Ext.Msg.OK
			});
			return;
		}
		if (records[0].get("c.auditing_State") == 1) {
        	Ext.Msg.show({
                title: '提示',
                msg: '不允许对审核通过的数据进行修改操作！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }
		if (records[0].get("c.auditing_State") == 4) {
        	Ext.Msg.show({
                title: '提示',
                msg: '不允许对撤销待审核的数据进行修改操作！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }
		if (records[0].get("c.auditing_State") == 5) {
        	Ext.Msg.show({
                title: '提示',
                msg: '不允许对报名通过(撤销未通过)的数据进行修改操作！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }
        var win = Ext.create('PJ.view.registrationmanagement.ChooseUserEdit',{
    		title: "修改报名信息",
    		flag:2,
    		grid: me,
    		userId:records[0].get("userId"),
    		openState: me.up('window').openState,//培训班状态
    		classId: me.up('window').classId,//培训班Id
    	}).show();
        win.down('form').getForm().load({
			url: me.store.preUpdateUrl,
            params: {
                searchUserId: records[0].get("userId")
            },
            success: function(baseForm, action){
//                baseForm.findField("tpassword").setValue(action.result.data.password);
            }
        });
        /*
        Ext.Msg.confirm({
            title: '提示',
            msg: '您确定要修改这些信息吗?',
            buttons: Ext.Msg.OKCANCEL,
            fn: function(id){
                if (id == 'ok') {
                    var classUserIds = new Array();
                    Ext.Array.each(records, function(name, index){
                    	classUserIds.push(name.get("classUserId"));
                    });
                    if(gj_onlineLearning_constant.loadType == "jsonp"){
                    	var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
                    		loadType: gj_onlineLearning_constant.loadType,
                    		url: me.getStore().updateUrl,
                    		extraParams: {
                    			searchClassUserIds: classUserIds,
                    			openState: me.up('window').openState
                    		}
                    	});
                    	store1.load(function(rec, operation, success){
                    		var msg = success == true ? '修改成功！' : operation.error;
                			Orchid.alert('消息', msg, function(){
            					var store = me.getStore();
            					store.load();
            				});
                    	});
                    }else if(gj_onlineLearning_constant.loadType == "ajax"){
                    	Ext.Ajax.request({
                    		url: me.getStore().updateUrl,
                    		params: {
                    			searchClassUserIds: classUserIds,
                    			openState: me.up('window').openState
                    		},
                    		success: function(response){
                    			var data = response.responseText;
                    			var json = Ext.decode(data, true);
                        		var msg = json.success == true ? '修改成功！' : json.msg;
                    			Orchid.alert('消息', msg, function(){
                					var store = me.getStore();
                					store.load();
                				});
                    		}
                    	});
                    }
                }
                else {
                    return;
                }
            }
        });
        */
    },
    //撤销报名
    onUnSignUpClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
        for (var i = 0; i < records.length; i++) {
            if (records[i].get("c.auditing_State") == 4) {
            	Ext.Msg.show({
                    title: '提示',
                    msg: '不允许对撤销待审核的数据执行撤销报名操作！',
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
                return;
            }
        }
        Ext.Msg.confirm({
            title: '提示',
            msg: '您确定要为这些学员撤销报名吗？',
            buttons: Ext.Msg.OKCANCEL,
            fn: function(id){
                if (id == 'ok') {
                    var classUserIds = new Array();
                    var userIds = new Array();
                    Ext.Array.each(records, function(name, index){
                    	classUserIds.push(name.get("classUserId"));
                    	userIds.push(name.get("userId"));
                    });
                    if(gj_onlineLearning_constant.loadType == "jsonp"){
                    	var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
                    		loadType: gj_onlineLearning_constant.loadType,
                    		url: me.getStore().deleteUrl,
                    		extraParams: {
                    			searchClassUserIds: classUserIds,
                    			searchUserIds: userIds,
                    			clickSource:me.clickSource,
                    			openState: me.up('window').openState
                    		}
                    	});
                    	store1.load(function(rec, operation, success){
                    		var msg = success == true ? '操作成功！' : operation.error;
                			Orchid.alert('消息', msg, function(){
            					var store = me.getStore();
            					store.load();
//                                me.quotaOfPeople();//单位人数统计
            				});
                    	});
                    }else if(gj_onlineLearning_constant.loadType == "ajax"){
                		
                    	Ext.Ajax.request({
                    		url: me.getStore().deleteUrl,
                    		params: {
                    			searchClassUserIds: classUserIds,
                    			searchUserIds: userIds,
                    			clickSource:me.up('window').clickSource,
                    			openState: me.up('window').openState
                    		},
                    		success: function(response){
                    			var data = response.responseText;
                    			var json = Ext.decode(data, true);
                        		var msg = json.success == true ? '操作成功！' : json.msg;
                    			Orchid.alert('消息', msg, function(){
                					var store = me.getStore();
                					store.load();
//                                    me.quotaOfPeople();//单位人数统计
                				});
                    		}
                    	});
                    }
                }
                else {
                    return;
                }
            }
        });
    },
    //班级人员数量统计
    quotaOfPeople: function(){
    	var me = this;
        var quotaOfPeopleLabel = this.down("#unitChooseQuotaOfPeople");
        var classId = me.classId;
        if (quotaOfPeopleLabel) {
            if(gj_onlineLearning_constant.loadType == "jsonp"){
//            	var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
//            		loadType: gj_onlineLearning_constant.loadType,
//            		url: me.store.unitQuotaOfPeopleUrl + '?searchClassId=' + classId
//            	});
            	store1.load(function(rec, operation, success){
            		if(success){
            			var jsonData = operation.response.result;
            			quotaOfPeopleLabel.setText(
            					'可报<span style="color:red">' + jsonData.asignCount +
            					'</span>人（市级<span style="color:red">' + jsonData.asignCityCount +
            					'</span>人，县级<span style="color:red">' + jsonData.asignCountyCount + 
            					'</span>人），已报<span style="color:red">' + jsonData.reportedCount +
            					'</span>人（市级<span style="color:red">' + jsonData.reportedCityCount +
            					'</span>人，县级<span style="color:red">' + jsonData.reportedCountyCount +
            					'</span>人），剩余<span style="color:red">' + jsonData.surplusCount +
            					'</span>人', false);
            		}
            	});
            }else if(gj_onlineLearning_constant.loadType == "ajax"){
            	Ext.Ajax.request({
            		url: me.store.unitQuotaOfPeopleUrl + '?searchClassId=' + classId,
            		success: function(response){
            			var data = response.responseText;
            			var json = Ext.decode(data, true);
            			if (json.success) {
            				var jsonData = json.data;
            				quotaOfPeopleLabel.setText(
            						'可报<span style="color:red">' + jsonData.asignCount +
                					'</span>人（市级<span style="color:red">' + jsonData.asignCityCount +
                					'</span>人，县级<span style="color:red">' + jsonData.asignCountyCount + 
                					'</span>人），已报<span style="color:red">' + jsonData.reportedCount +
                					'</span>人（市级<span style="color:red">' + jsonData.reportedCityCount +
                					'</span>人，县级<span style="color:red">' + jsonData.reportedCountyCount +
                					'</span>人），剩余<span style="color:red">' + jsonData.surplusCount +
                					'</span>人', false);
            			}
            		}
            	});
            }
        }
    },
    listeners: {
    	selectionchange: function(selModel, selected){
    		if(!this.up("window").isSignUp){
    			this.down('#' + this.oldId + "UnSignUp").setDisabled(selected.length === 0);
    		}
    	},
        viewready: function(){
        	var me = this;
        	var items = {};
        	if(me.up("window").isSignUp){
        		items = {
                        xtype: 'toolbar',
                        dock: 'top',
                        displayInfo: true,
                        items: [{
                            text: '报名',
                            iconCls: 'addIco',
                            itemId: this.oldId + "Add",
                            scope: this,
                            handler: this.onAddClick,
                            hidden: !me.up("window").isSignUp
                        }, "-", {
                            text: '修改',
                            iconCls: 'updateIco',
                            itemId: this.oldId + "Update",
                            disabled: true,
                            scope: this,
                            handler: this.onUpdateClick,
                            hidden: !me.up("window").isSignUp
                        }/*,  "-", {
                            xtype: 'label',
                            itemId: 'unitChooseQuotaOfPeople',
                            html: ''
                        }*/, "->",this.countSelectPanel]
                    };
        	}else{
        		items = {
                        xtype: 'toolbar',
                        dock: 'top',
                        displayInfo: true,
                        items: [{
                            text: '撤销报名',
                            iconCls: 'publishCancelIco',
                            itemId: this.oldId + "UnSignUp",
                            disabled: true,
                            scope: this,
                            handler: this.onUnSignUpClick,
                            hidden: me.up("window").isSignUp
                        }/*,  "-", {
                            xtype: 'label',
                            itemId: 'unitChooseQuotaOfPeople',
                            html: ''
                        }*/, "->",this.countSelectPanel]
                    };
        	}
        	
            this.addDocked([items]);
            // 触发权限过滤事件
            this.fireEvent("permission", this);
            var store = this.store;
            var params = this.store.proxy.extraParams;
            var newParams = {
            		searchClassID: this.up('window').classId,
        			clickSource:me.up('window').clickSource,
        			searchSignUpState:me.up('window').auditingState
            };
            Ext.apply(params, newParams);
            store.isLoad = true;
            store.load(function(rec, operation, success){
//            	me.quotaOfPeople();//人数统计
            });
        }
    }
});
